package com.echo.yixiaowu.util

import android.app.Activity
import android.content.Context
import android.provider.Settings
import androidx.activity.compose.LocalActivity
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.core.view.WindowCompat


object SystemUtil {
    private var statusBarHeight = 0

    private var navigateBarHeight = 0

    fun init(statusBar: Int, navigationBar: Int) {
        statusBarHeight = statusBar
        navigateBarHeight = navigationBar
    }

    fun setStatusBarTheme(
        activity: Activity,
        isDarkColor: Boolean,
    ) {
        val window = activity.window
        val controller = WindowCompat.getInsetsController(window, window.decorView)
        window.decorView.post {
            controller.isAppearanceLightStatusBars = isDarkColor
            controller.isAppearanceLightNavigationBars = isDarkColor
        }
    }

    fun isAutoRotateEnabled(context: Context): Boolean {
        return Settings.System.getInt(
            context.contentResolver,
            Settings.System.ACCELEROMETER_ROTATION,
            0
        ) == 1
    }

    fun getStatusBarTopHeight(density: Density): Dp {
        return with(density) {
            statusBarHeight.toDp()
        }
    }

    private fun getNavigateBarHeightDp(density: Density): Dp {
        return with(density) { navigateBarHeight.toDp() }
    }


    @Composable
    fun isOrientationPortrait(): Boolean {
        val activity = LocalActivity.current
        return activity?.isOrientationPortrait() == true
    }

    fun getSystemBarHeightDp(density: Density): Dp {
        return getStatusBarTopHeight(density) + getNavigateBarHeightDp(density)
    }
}
